/*

 Test to see if we can parse characters such as "+" or "%" off of a given
 command-line argument.

 This will be important to make setbrightness a scriptable program.

 Copyright 2022 kzimmermann - https://tilde.town/~kzimmermann

 Licensed under the GNU GPL v3 - https://www.gnu.org/licenses

 */

#include <stdio.h>
#include <stdlib.h>

// datatype of a "tuple" of a leading character and a numerical value:
struct argtuple {
    char sign;
    int value;
};

// this function will process a token like "+83239" and identify the parts:
struct argtuple
parseArg(char token[])
{
    struct argtuple t;
    sscanf(token, "%1c%d", &t.sign, &t.value);
    if (!(t.sign == '+' || t.sign == '-')) {
        // parsing error. Let's use the (!,0) tuple as an error code
        printf("Warning: argument does not contain a sign!\n");
        t.sign = '!';
        t.value = 0;
    }
    return t;
}

int
main(int argc, char *argv[])
{
    if (argc == 1) {
        printf("Pass an argument containing a char and an int (+3424, etc)\n");
        return 1;
    }

    struct argtuple at;

    for (int i = 1; i < argc; ++i) {
        at = parseArg(argv[i]);
        if (at.sign == '!' && at.value == 0) {
            printf("Error scanning argument #%d.\n", i);
        }
        else {
            printf("Argument #%d: { %c, %d}\n", i, at.sign, at.value);
        }
    }

    return 0;
}
